/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.isip.transformation;

import java.util.ArrayList;

/**
 *
 * @author Ahmad Fauzan
 */
public class ScaleTranformation {
    
    int x1,y1;
    int x2,y2;
    
    int xs1,ys1;
    int xs2,ys2;
    int xs3,ys3;
    int xs4,ys4;
    
    public int[][] doTransform(int[][] matrix, int[] source, 
            int[] target) {
        int[][] res = new int[matrix.length][matrix[0].length];
        for(int i=0; i < res.length; i++) {
            for(int j=0; j < res[i].length; j++) {
                res[i][j] = matrix[i][j];
            }
        }
        if(source.length == 4 && target.length == 8) {
            int height = source[3] - source[1];
            int source_x_0 = source[0];
            int source_y_0 = source[1];
            int source_x_1 = source[2];
            int source_y_1 = source[3];
            
            // target
            int x_0 = target[0];
            int y_0 = target[1];
            int x_1 = target[2];
            int y_1 = target[3];
            int x_2 = target[4];
            int y_2 = target[5];
            int x_3 = target[6];
            int y_3 = target[7];
            
            int heightLeft = y_2 - y_0;
            int heightRight = y_3 - y_1;
            
            int yLeft = y_0;
            int yRight = y_1;
            
            while(yLeft < y_2 && yRight < y_3) {
                int xSource0 = source_x_0;
                int xSource1 = source_x_1;
                int ySource = source_y_0;
                
                double leftScale = (yLeft-y_0) / (double)heightLeft;
                double rightScale = (yRight-y_1) / (double) heightRight;
                double scale = leftScale > rightScale ? rightScale : leftScale;
                
                int y0 = ySource + (int)(height*scale);
                Integer[] colors = pickColorByLine(matrix, new int[] {xSource0,y0}, 
                        new int[] {xSource1,y0});
                putColorByLine(res, new int[] {x_0,yLeft}, new int[]{x_1,yRight}, colors);
                
                if(leftScale < rightScale) {
                    yLeft++;
                } else {
                    yRight++;
                }
            }
        }
        return res;
    }
    
    public void putColorByLine(int[][] matrix, int[] pos0, int[] pos1, Integer[] plot) {
        int x0 = pos0[0];
        int y0 = pos0[1];
        int x1 = pos1[0];
        int y1 = pos1[1];
        int deltax = x1-x0;
        int deltay = y1-y0;
        double err = 0;
        double deltaerror = deltax > 0 ? deltay/(double)deltax : 0;
        int y = y0;
        for(int x=x0; x <= x1; x++) {
            matrix[y][x] = plot[(int)((x-x0)/(double)deltax*(plot.length-1))];
            err += deltaerror;
            if(err >= 0.5) {
                y++;
                err -= 1.0;
            } else if(err <= -0.5) {
                y--;
                err += 1.0;
            }
        }
    }
    
    public Integer[] pickColorByLine(int[][] matrix, int[] pos1, int[] pos2) {
        ArrayList<Integer> colors = new ArrayList<>();
        int x0 = pos1[0];
        int y0 = pos1[1];
        int x1 = pos2[0];
        int y1 = pos2[1];
        int deltax = x1-x0;
        int deltay = y1-y0;
        double err = 0;
        double deltaerror = deltax > 0 ? deltay/(double)deltax : 0;
        int y = y0;
        for(int x=x0; x <= x1; x++) {
            colors.add(matrix[y][x]);
            err += deltaerror;
            if(err >= 0.5) {
                y++;
                err -= 1.0;
            } else if(err <= -0.5) {
                y--;
                err += 1.0;
            }
        }
        return colors.toArray(new Integer[colors.size()]);
    }
    
}
